package com.itheima.controller;


import com.github.pagehelper.Page;
import com.itheima.pojo.Blog;
import com.itheima.service.BlogService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/content")
@Api(tags = "文章详情相关接口")
@CrossOrigin
public class ContentController {
    @Autowired
    private BlogService blogService;

    @GetMapping("/getBlogByUid/{uid}")
    @ApiOperation("通过Uid获取博客内容")
    public String getBlogbyUid(@PathVariable String uid){
        if(uid==null||"".equals(uid)){
            return ResultUtil.result(401 , MessageConstant.ADD_UID);
        }
        Blog blog=blogService.getBlogByUid(uid);
        if(blog==null){
            return ResultUtil.result("401",MessageConstant.CONTENT_SELECT_FAIL);
        }
        return ResultUtil.result("200",blog);
    }

    @GetMapping("/getBlogPraiseCountByUid/{uid}")
    @ApiOperation("通过uid获取博客点赞数")
    public String getBlogPraiseCountByUid(@PathVariable String uid){
        Integer count = blogService.getBlogPraiseCountByUid(uid);
        if(count==null){
            return ResultUtil.result("401",MessageConstant.CONTENT_SELECT_FAIL);
        }
        return ResultUtil.result("200",count);
    }

    @GetMapping("/getSameBlogByBlogUid/{blogUid}")
    @ApiOperation("根据bloguid获取同类型的博客")
    public String getSameBlogByBlogUid(@PathVariable String blogUid){
        List<Blog> blogList = blogService.getSameBlogByBlogUid(blogUid);
        if (blogList==null){
            return ResultUtil.result("401",MessageConstant.CONTENT_SELECT_FAIL);
        }
        return ResultUtil.result("200",blogList);
    }

    @GetMapping("/getSameBlogByTagUid/{tagUid}/{currentPage}/{pageSize}")
    @ApiOperation("根据标签uid获取相关的博客")
    public String getSameBlogByTagUid(@PathVariable("tagUid")String tagUid,@PathVariable("currentPage")Long currentpagr,@PathVariable("pageSize")Long pageSize){
        if(currentpagr<=0L||pageSize<=0L){
            currentpagr=1L;
            pageSize=1L;
        }
        Page<Blog> blogList = blogService.getSameBlogByTagUid(tagUid, currentpagr, pageSize);
        if (blogList==null){
            return ResultUtil.result("401",MessageConstant.TAG_NULL_FAIL);
        }
        return ResultUtil.result("200",blogList);
    }

    @GetMapping("/praiseBlogByUid/{uid}")
    @ApiOperation("通过uid给博客点赞")
    public String praiseBlogByUid(@PathVariable String uid){
        return blogService.praiseBlogByUid(uid);
    }
}
